package com.hdsxtech.smsoot_timer.task;

 
import com.hdsxtech.smsoot_timer.entity.TestA;
import com.hdsxtech.smsoot_timer.entity.TestB;
import com.hdsxtech.smsoot_timer.service.TestAService;
import com.hdsxtech.smsoot_timer.service.TestBService;
import org.aspectj.weaver.ast.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@EnableScheduling
@Component
public class ScheduledTasks {

    private final static Logger LOGGER = LoggerFactory.getLogger(ScheduledTasks.class);

   // private TestBService bestBService = SpringContextHolder.getBean(TestBService.class);

   @Autowired
   TestBService bestBService;
   
   @Autowired
   TestAService testAService;

    @Scheduled(fixedRate = 10000)
    public void tasks(){
        LOGGER.info("----------定时任务执行--------"+System.currentTimeMillis());
        
        List<TestB> testbList = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			TestB b = new TestB();
			b.setPId(UUID.randomUUID().toString());
			b.setvName("测试多数据源"+i);
			b.setvContext("这是为了测试多数据怎么样，并看看功能怎么样"+i);
			testbList.add(b);
		}
		 
		bestBService.save(testbList);
        
		List<TestB> retList  =bestBService.queryAll();
        List<TestA> testaList = new ArrayList<>();
        for (TestB b : retList) {
        	TestA a = new TestA();
        	a.setpId(b.getPId());
        	a.setvName(b.getvName());
        	a.setvContext(b.getvContext());
        	a.setDrFlag(b.getDrFlag());
        	a.setMvFlag(1);
        	a.setCreateTime(b.getCreateTime());
        	testaList.add(a);
		}
        
		testAService.save(testaList );
		bestBService.update(retList);
		 LOGGER.info("----------定时任务执行完成--------"+System.currentTimeMillis());
    }
}
